//
//  QFreeCertificateVC.m
//  QoilPublic
//
//  Created by 杨凯 on 16/9/5.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QFreeCertificateVC.h"
#import "QFreeCertificateCell.h"
#import "QGrabCertificateVC.h"
#import "QGrabCertificateVC.h"
@interface QFreeCertificateVC ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation QFreeCertificateVC{
    
    UITableView *_tableView;
    NSArray *_arr;
}
- (id)init{
    self = [super init];
    if (self) {
        
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"免费办证";
    [self createTableView];
    [self requestData];
}


- (void)createTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    [_tableView registerNib:[UINib nibWithNibName:@"QFreeCertificateCell" bundle:nil] forCellReuseIdentifier:@"QFreeCertificateCell"];
}

#pragma mark --- tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QFreeCertificateCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QFreeCertificateCell" forIndexPath:indexPath];
    cell.indexPath = indexPath;
    cell.block = ^(NSInteger index){
        
        CLog(@"index===%ld",(long)index);
        QGrabCertificateVC *vc = [[QGrabCertificateVC alloc] init];
        vc.dics = _arr[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    };
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if(_arr.count){
        cell.dics = _arr[indexPath.row];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 80;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (void)requestData{
    NSString *url = [QParamService urlWithAction:@"selCertificates"];
    NSDictionary *param = [QParamService freeCertifiCateListpageNum:@"0" pageSize:KpageSize];
    [QViewUtility showMBHudToView:self.view hideBg:YES];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            NSDictionary *dic = basModel.items;
            _arr = dic[@"list"];
            [_tableView reloadData];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            if([basModel.errMsg isEqualToString:@"请重新登录"]){
                [QDataUtility deleteLoginModel];
            }
        }
    }];
}



@end
